package com.collect.tanzania.zhiUtils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;

import androidx.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;

import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectConstants;

import java.util.Locale;

public class LanguageUtils {

    private static final String TAG = "LanguageUtils";
    private static Resources mResources;
    private static String country = null;
    private static Context mContext = null;
    private static String mLanguage = "";
    private static Locale mDefaultLocale;

    public static void init(Context context) {
        initResources(context);
        country = context.getResources().getConfiguration().locale.getCountry();

        mContext = context;
        String language = SPUtils.getInstance().getString(ZHIProjectConstants.APP_LANGUAGE);
        if(true == TextUtils.isEmpty(language)){
            language = LanguageType.ENGLISH.getLanguage();
        }
        changeLanguage(language);
    }

    public static void initResources(Context context) {
        mResources = context.getResources();
    }

    public static void changeLanguage(String language) {

        Configuration config = mResources.getConfiguration();     // 获得设置对象
        DisplayMetrics dm = mResources.getDisplayMetrics();

        if(LanguageType.CHINESE.getLanguage().equalsIgnoreCase(language)){
            config.locale = Locale.SIMPLIFIED_CHINESE;     // 中文
            config.setLayoutDirection(Locale.SIMPLIFIED_CHINESE);
            mLanguage = "zh-CN";
            country = "CN";
            SPUtils.getInstance().put(ZHIProjectConstants.APP_LANGUAGE, language);
        }
        else if(LanguageType.ENGLISH.getLanguage().equalsIgnoreCase(language)){
            config.locale = Locale.ENGLISH;   // 英文
            config.setLayoutDirection(Locale.ENGLISH);
            mLanguage = "en";
            country = "US";
            SPUtils.getInstance().put(ZHIProjectConstants.APP_LANGUAGE, language);
        }
        else {
            country = mContext.getResources().getConfiguration().locale.getCountry();

            if ("CN".equals(country)){
                mDefaultLocale = Locale.SIMPLIFIED_CHINESE;
                mLanguage = "zh-CN";
                country = "CN";
            }else {
                mDefaultLocale =  Locale.ENGLISH;
                mLanguage = "en";
                country = "US";
            }

            config.locale = mDefaultLocale;         // 系统默认语言
            config.setLayoutDirection(mDefaultLocale);
            SPUtils.getInstance().put(ZHIProjectConstants.APP_LANGUAGE, language);
        }

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            LanguageUtils.applyChange();
        }
    }

    /**
     * 获取当前字符串资源的内容
     */
    public static String getStringById(int id) {
        String string;
        if (false == TextUtils.isEmpty(mLanguage)) {
            string = mResources.getString(id, mLanguage);
        } else {
            string = mResources.getString(id, "");
        }

        if (false == TextUtils.isEmpty(string)) {
            return string;
        }
        return "";
    }

    public static Locale getSetLocale() {

        String language = SPUtils.getInstance().getString(ZHIProjectConstants.APP_LANGUAGE);
        if(LanguageType.CHINESE.getLanguage().equalsIgnoreCase(language)){
            return Locale.CHINA;
        }
        else if(LanguageType.ENGLISH.getLanguage().equalsIgnoreCase(language)){
            return Locale.ENGLISH;
        }
        else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return Resources.getSystem().getConfiguration().getLocales().get(0);//解决了获取系统默认错误的问题
            } else {
                return Locale.getDefault();
            }
        }
    }

    public static void applyChange() {
        Resources res = mContext.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();

        Locale locale = getSetLocale();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            conf.setLocale(locale);
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            conf.setLocales(localeList);
        } else {
            conf.locale = locale;
            try
            {
                conf.setLayoutDirection(locale);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        res.updateConfiguration(conf, dm);
    }

    /////////////////////////////////////////////////////////////////////

    /**
     * @param context
     * @param newLanguage 想要切换的语言类型 比如 "en" ,"zh"
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @SuppressWarnings("deprecation")
    public static void changeAppLanguage(Context context, String newLanguage) {
        if (TextUtils.isEmpty(newLanguage)) {
            return;
        }
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        //获取想要切换的语言类型
        Locale locale = getLocaleByLanguage(newLanguage);
        configuration.setLocale(locale);
        // updateConfiguration
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static Locale getLocaleByLanguage(String language) {
        Locale locale = Locale.SIMPLIFIED_CHINESE;
        if (language.equals(LanguageType.CHINESE.getLanguage())) {
            locale = Locale.SIMPLIFIED_CHINESE;
        } else if (language.equals(LanguageType.ENGLISH.getLanguage())) {
            locale = Locale.ENGLISH;
        } else if (language.equals(LanguageType.THAILAND.getLanguage())) {
            locale = Locale.forLanguageTag(language);
        }
        Log.d(TAG, "getLocaleByLanguage: " + locale.getDisplayName());
        return locale;
    }

    public static Context attachBaseContext(Context context, String language) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language);
        } else {
            return context;
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language) {
        Resources resources = context.getResources();
        Locale locale = LanguageUtils.getLocaleByLanguage(language);

        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        return context.createConfigurationContext(configuration);
    }

    public enum LanguageType {

        CHINESE("ch"),
        ENGLISH("en"),
        THAILAND("th"),
        DEFAULT("default");

        private String language;

        LanguageType(String language) {
            this.language = language;
        }

        public String getLanguage() {
            return language == null ? "" : language;
        }
    }
}
